/**
 * Project: Daycare Web Application Package: com.made2ordersoftware.daycare.service File:
 * DaoFactory.java Created: Jun 29, 2010
 */
/**
 * Copyright (c) 2010 Made 2 Order Software, LLC
 *
 * All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 * transmitted in any form or by any means, without the prior written permission of Made 2 Order
 * Software.
 *
 * You may not distribute the URL or redistribute the files to third parties. You may transfer the
 * files onto a single storage device so long as you can prove ownership of the software. You may
 * not reverse engineer, decompile, or disassemble the software or media files. You may not modify
 * or translate the software or media, or distribute copies of the software or media without the
 * written consent of Made 2 Order Software, LLC.
 */
package com.made2ordersoftware.service;

import com.made2ordersoftware.service.exceptions.DatabaseFactoryException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * DAO Factory class user to dynamically create data access objects.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
public class DaoFactory {

   private static final Logger LOG = LoggerFactory.getLogger(DaoFactory.class);

   /**
    * Factory method to get a concrete instance of an AbstractDao class.
    *
    * @param name Name of concrete class being instantiated.
    *
    * @return An concrete instance of AbstractDao.
    *
    * @throws DatabaseFactoryException For errors associated with the factory connection of a
    *                                  transaction.
    */
   public static AbstractDao getDaoInstance(String name) throws DatabaseFactoryException {
      LOG.info("Getting instance of DAO Factory.");
      AbstractDao retobj = null;
      try {
         Class<?> cls = Class.forName(name);
         Class partypes[] = new Class[1];
         partypes[0] = String.class;
         Constructor ct = cls.getConstructor(partypes);
         retobj = (AbstractDao) ct.newInstance(new Object[]{"datasource"});
         LOG.info("DAO instance creation successful.");
      } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
              IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
         LOG.info("Error creating DAO instance.");
         throw new DatabaseFactoryException("com.made2ordersoftware.dao:DaoFactory:getDaoInstance:"
                 + e.getCause() + ":", e);
      }
      return retobj;
   }
}
